package com.sssoft.Yundian.Dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.sssoft.Yundian.bean.Goods;
import com.sssoft.Yundian.bean.Other;
import com.sssoft.Yundian.bean.Pgoods;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;

public class GoodsDao {
	private MyHelper helper;

	public GoodsDao(Context context) {
		helper = new MyHelper(context);
	}

	public List<Goods> queryAll() {
		HashMap<Integer, String> types = get_type();
		HashMap<Integer, String> utils = get_units();
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.query("goods", null, null, null, null, null, "goods_id Desc");
		List<Goods> list = new ArrayList<Goods>();
		while (cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("goods_id"));
			String name = cursor.getString(cursor.getColumnIndex("goods_name"));
			int type_id = cursor.getInt(cursor.getColumnIndex("goods_type_id"));
			String spec = cursor.getString(cursor.getColumnIndex("goods_spec"));
			int units_id = cursor.getInt(cursor.getColumnIndex("goods_units_id"));
			String price = cursor.getString(cursor.getColumnIndex("goods_price"));
			String num = cursor.getString(cursor.getColumnIndex("goods_num"));
			String remark = cursor.getString(cursor.getColumnIndex("goods_remark"));
			String sales = cursor.getString(cursor.getColumnIndex("goods_sales"));
			String img = cursor.getString(cursor.getColumnIndex("goods_img"));
			String date = cursor.getString(cursor.getColumnIndex("goods_date"));
			String type = types.get(type_id);
			String unit = utils.get(units_id);
			list.add(new Goods(id, name, type, spec, unit, price, num, remark, sales, img, date));
		}
		cursor.close();
		db.close();
		return list;
	}

	public List<Goods> findMarkGood(long porders_id, HashMap<Integer, Integer> pgoods) {
		HashMap<Integer, String> types = get_type();
		HashMap<Integer, String> utils = get_units();
		List<Goods> list = new ArrayList<Goods>();
		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.rawQuery("select * from goods where goods_id in (select goods_id from put_goods where porders_id=?)", new String[] {porders_id+""});
		while (cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("goods_id"));
			String name = cursor.getString(cursor.getColumnIndex("goods_name"));
			int type_id = cursor.getInt(cursor.getColumnIndex("goods_type_id"));
			String spec = cursor.getString(cursor.getColumnIndex("goods_spec"));
			int units_id = cursor.getInt(cursor.getColumnIndex("goods_units_id"));
			String price = cursor.getString(cursor.getColumnIndex("goods_price"));
			String num = cursor.getString(cursor.getColumnIndex("goods_num"));
			String remark = cursor.getString(cursor.getColumnIndex("goods_remark"));
			String sales = cursor.getString(cursor.getColumnIndex("goods_sales"));
			String img = cursor.getString(cursor.getColumnIndex("goods_img"));
			String date = cursor.getString(cursor.getColumnIndex("goods_date"));
			String type = types.get(type_id);
			String unit = utils.get(units_id);
			Goods a = new Goods(id, name, type, spec, unit, price, num, remark, sales, img, date);
			a.setSelected(true);
			a.setSelectedNum(pgoods.get(a.getGoods_id()));
			list.add(a);
		}
		
		cursor.close();
		db.close();
		return list;

	}
	
	@SuppressLint("UseSparseArrays")
	public HashMap<Integer, String> get_type() {
		HashMap<Integer, String> types = new HashMap<Integer, String>();

		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.query("goods_type", null, null, null, null, null, null);
		while (cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("goods_type_id"));
			String goods_type = cursor.getString(cursor.getColumnIndex("goods_type_name"));
			types.put(id, goods_type);
		}
		cursor.close();
		db.close();
		return types;
	}

	
	@SuppressLint("UseSparseArrays")
	public HashMap<Integer, String> get_units() {
		HashMap<Integer, String> units = new HashMap<Integer, String>();

		SQLiteDatabase db = helper.getReadableDatabase();
		Cursor cursor = db.query("goods_units", null, null, null, null, null, null);
		while (cursor.moveToNext()) {
			int id = cursor.getInt(cursor.getColumnIndex("goods_units_id"));
			String goods_units = cursor.getString(cursor.getColumnIndex("goods_units_name"));
			units.put(id, goods_units);
		}
		cursor.close();
		db.close();
		return units;

	}
	
	
	public boolean register(String et_goodname,String et_goodspecification,String et_goodvalue,String et_goodnumber,String et_goodnote){
        SQLiteDatabase db= helper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("goods_name",et_goodname);
        values.put("goods_spec",et_goodspecification);
        values.put("goods_price",et_goodvalue);
        values.put("goods_sales",et_goodnumber);
        values.put("goods_remark",et_goodnote);
        
        db.insert("goods",null,values);
        db.close();
        return true;
    }
    
    public static boolean hasSdcard() {
		String state = Environment.getExternalStorageState();
		if (state.equals(Environment.MEDIA_MOUNTED)) {
			// �д洢��SDCard
			return true;
		} else {
			return false;
		}
	}
    public boolean addgoods(String type){
        SQLiteDatabase db= helper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("goods_type_id",type);
        
        db.insert("goods",null,values);
        db.close();
        return true;
    }
    public boolean addgoodss(String units){
        SQLiteDatabase db= helper.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put("goods_units_id",units);
        
        db.insert("goods",null,values);
        db.close();
        return true;
    }
    
    public String[] queryAllType(){
        SQLiteDatabase db=helper.getReadableDatabase();
        Cursor c=db.query("goods_type",null,null,null,null,null,"goods_type_id");
        List<Other> list=new ArrayList<Other>();
        while (c.moveToNext()){
            long id=c.getLong(c.getColumnIndex("goods_type_id"));
            String name=c.getString(1);
            list.add(new Other(id,name));
        }
        c.close();
        db.close();
        String[] a = new String[list.size()];
        for(int i=0;i<list.size();i++) {
        		a[i] = list.get(i).getName();
        }
        return a;
    }
    
    public String[] queryAllunits(){
        SQLiteDatabase db=helper.getReadableDatabase();
        Cursor c=db.query("goods_units",null,null,null,null,null,"goods_units_id");
        List<Other> list=new ArrayList<Other>();
        while (c.moveToNext()){
            long id=c.getLong(c.getColumnIndex("goods_units_id"));
            String name=c.getString(1);
            list.add(new Other(id,name));
        }
        c.close();
        db.close();
        String[] a = new String[list.size()];
        for(int i=0;i<list.size();i++) {
        		a[i] = list.get(i).getName();
        }
        return a;
    }
	

}  
